﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Text;
using WinnerCMS.Common;
using System.Net;

#endregion

namespace WinnerCMS.BLL
{
    public class FriendLinkCheck
    {
        private readonly string Url = "";

        public FriendLinkCheck(string url)
        {
            this.Url = url;
        }

        /// <summary>
        /// 链接检查
        /// </summary>
        /// <param name="link"></param>
        /// <returns></returns>
        public string CheckLink(string link)
        {
            string strMessage = "";
            string strUrl = "http://" + this.Url;
            NetHelper text = new NetHelper {TimeOut = 1000};
            RemoteRes info = text.Get(strUrl);
            if (info.StatusCode == HttpStatusCode.OK)
            {
                string str2 = info.HTML;
                if (str2.IndexOf(link, StringComparison.CurrentCultureIgnoreCase) > -1)
                {
                    strMessage = "错链";
                    if (str2.IndexOf("http://" + link, StringComparison.CurrentCultureIgnoreCase) > -1)
                    {
                        strMessage = "直链";
                    }
                    return strMessage;
                }

                strMessage = "无链";
                return strMessage;
            }

            strMessage = info.HTML;
            return strMessage;
        }

        /// <summary>
        /// Alexa
        /// </summary>
        /// <returns></returns>
        public int GetAlexa()
        {
            string strUrl = "http://www.alexa.com/siteinfo/" + this.Url.Replace("http://", string.Empty);
            NetHelper text = new NetHelper {TimeOut = 1000};
            RemoteRes info = text.Get(strUrl);
            if (info.StatusCode != HttpStatusCode.OK)
            {
                return -1;
            }
            string str2 =
                MyString.GetInnerString(info.HTML,
                    "<a class=\"data-color2 \" href=\"/site/linksin/" + this.Url.Replace("http://", string.Empty) +
                    "\">", "</a>").Trim();
            if (str2.Length == 0)
            {
                return 0;
            }
            try
            {
                return Convert.ToInt32(str2);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// BaiDu
        /// </summary>
        /// <returns></returns>
        public int GetBaiDu()
        {
            string strUrl = "http://www.baidu.com/s?cl=3&wd=site%3A" + this.Url;
            NetHelper text = new NetHelper {TimeOut = 1000};
            RemoteRes info = text.Get(strUrl);
            if (info.StatusCode != HttpStatusCode.OK)
            {
                return -1;
            }
            if (info.HTML.Contains("没有找到"))
            {
                return 0;
            }

            string str2 = MyString.GetInnerString(info.HTML, "百度一下，找到相关网页", "篇，用时").Replace(",", "").Replace("约", "");
            try
            {
                return Convert.ToInt32(str2);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Google
        /// </summary>
        /// <returns></returns>
        public int GetGoogle()
        {
            string strUrl = "http://www.google.com.hk/search?complete=1&hl=zh-CN&meta=&q=site%3A" + this.Url;
            NetHelper text = new NetHelper {TimeOut = 1000};
            RemoteRes info = text.Get(strUrl);
            if (info.StatusCode != HttpStatusCode.OK)
            {
                return 0;
            }
            string str2 = MyString.GetInnerString(info.HTML, "约有 <b>", "</b> 项符合").Replace(",", "");
            try
            {
                return Convert.ToInt32(str2);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// SoGou
        /// </summary>
        /// <returns></returns>
        public int GetSoGou()
        {
            string strUrl = "http://www.sogou.com/web?query=site%3A" + this.Url;
            NetHelper text = new NetHelper {TimeOut = 1000};
            RemoteRes info = text.Get(strUrl);
            if (info.StatusCode != HttpStatusCode.OK)
            {
                return -1;
            }
            string str2 = MyString.GetInnerString(info.HTML, "<!--resultbarnum:", "-->个网页").Replace(",", "");
            try
            {
                return Convert.ToInt32(str2.Trim());
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// SoSo
        /// </summary>
        /// <returns></returns>
        public int GetSoSo()
        {
            string strUrl = "http://www.soso.com/q?sc=web&ch=w.ptl&lr=&w=site%3A" + this.Url;
            NetHelper text = new NetHelper {TimeOut = 1000};
            RemoteRes info = text.Get(strUrl);
            if (info.StatusCode != HttpStatusCode.OK)
            {
                return -1;
            }
            if (info.HTML.Contains("找不到"))
            {
                return 0;
            }
            string str2 = MyString.GetInnerString(info.HTML, "搜索到约", "项结果").Replace(",", "");
            try
            {
                return Convert.ToInt32(str2);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Yahoo
        /// </summary>
        /// <returns></returns>
        public int GetYahoo()
        {
            string strUrl = "http://search.cn.yahoo.com/search?p=site%3A" + this.Url;
            NetHelper text = new NetHelper {TimeOut = 1000};

            RemoteRes info = text.Get(strUrl);
            if (info.StatusCode != HttpStatusCode.OK)
            {
                return -1;
            }

            string str2 = MyString.GetInnerString(info.HTML, "找到相关网页约", "条").Replace(",", "");
            try
            {
                return Convert.ToInt32(str2);
            }
            catch
            {
                return 0;
            }
        }
    }
}